//
//  WJMasonryController.m
//  JerryIos
//
//  Created by 王军 on 2018/2/17.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJMasonryController.h"
#import "WJItem.h"
#import "MASViewController.h"

@interface WJMasonryController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *itemArray;

@end

@implementation WJMasonryController

- (NSArray *)itemArray {
    if (_itemArray == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"masonry" ofType:@"plist"];
        NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *mutItemArray = [NSMutableArray array];
        for (NSDictionary *dic in dicArray) {
            WJItem *item = [WJItem itemWithDict:dic];
            [mutItemArray addObject:item];
        }
        _itemArray = mutItemArray;
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 创建tableView
    [self setupTableView];
}

// 创建tableView
- (void)setupTableView {
    
    //TableView的Frame
    CGFloat topHeight = [[UIApplication sharedApplication] statusBarFrame].size.height + 44;
    self.tableView.frame = CGRectMake(0, topHeight, self.view.bounds.size.width, self.view.bounds.size.height - topHeight);
    //指定代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellId];
    }
    WJItem *item = self.itemArray[indexPath.row];
    cell.textLabel.text = item.title;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WJItem *item = self.itemArray[indexPath.row];
    Class class = NSClassFromString(item.controller);
    UIViewController *controller = [[MASViewController alloc] initWithTitle:item.title viewClass:class];
    controller.navigationItem.title = item.title;
    [self.navigationController pushViewController:controller animated:YES];
    
}

@end
